package cn.learn.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 测试一下用户的权限
 *
 * @author huangyezhan
 * @date 2020年02月15日16:22
 */
@Api(value = "测试机", tags = {"测试标签接口"})
@RestController
@RequestMapping("/order/")
public class OderDemoController {

    @Autowired
    private TokenStore tokenStore;

    /**
     * 从jwt中获取用户的个人信息
     *
     * @param token jwt的token
     * @return 返回结果
     */
    @RequestMapping(value = "getUser", method = RequestMethod.GET)
    public OAuth2Authentication getUser(String token) {
        return tokenStore.readAuthentication(token);
    }

    @ApiOperation(value = "查询有效标志的质量维度数据")
    @RequestMapping(value = "placeOrder", method = RequestMethod.GET)
    public Map placeOrder(@RequestParam("addOrder") String placeOrder, @RequestParam(required = false) String name) {
        System.out.println("进入业务方法");
        Map<String, Object> map = new HashMap<>(16);
        map.put("订单状态", "成功");
        //int i = 1 / 0;
        System.out.println("执行完毕业务方法，准备返回数据");
        return map;
    }


    @RequestMapping(value = "cancelOrder", method = RequestMethod.GET)
    public Map cancelOrder() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("撤销订单状态", "失败");
        //int i = 1 / 0;
        return map;
    }


    @RequestMapping(value = "deleteOrder", method = RequestMethod.GET)
    public OAuth2Authentication deleteOrder(String token) {
        OAuth2AccessToken accessToken = tokenStore.readAccessToken(token);
        if (accessToken == null) {
            throw new InvalidTokenException("该令牌不存在");
        }
        if (accessToken.isExpired()) {
            throw new InvalidTokenException("令牌已经过期");
        }

        OAuth2Authentication authentication = tokenStore.readAuthentication(token);

        Collection<GrantedAuthority> authorities = authentication.getAuthorities();

        OAuth2Request request     = authentication.getOAuth2Request();
        Set<String>   resourceIds = request.getResourceIds();
        Set<String>   scope       = request.getScope();


        List<String> collect = authorities.stream().map(e -> e.getAuthority()).collect(Collectors.toList());
        return tokenStore.readAuthentication(token);
        /*Map<String, Object> map = new HashMap<>(16);
        map.put("删除订单状态", "请联系管理员!");
        return map;*/
    }


    //@PreAuthorize("hasAnyAuthority('testOrder')")
    @RequestMapping(value = "testOrder", method = RequestMethod.GET)
    public Map testOrder() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object         principal      = authentication.getPrincipal();
        //加密的令牌
        Object              details = authentication.getDetails();
        String              name    = authentication.getName();
        Map<String, Object> map     = new HashMap<>(16);
        map.put("测试订单", "测试订单_成功");
        map.put("principal", principal);
        map.put("name", name);
        return map;
    }


    @RequestMapping(value = "test", method = RequestMethod.GET)
    public Map test() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("测试_没权限接口", "成功!");
        return map;
    }


}
